package com.bhh.bio;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author bhh
 * @description 于BIO模型, 配合客户端创建线程处理控制台输入(System.in会让线程阻塞)
 * 和 ChatClient 类
 * 和 ChatServer 类 + ChatHandler类配合使用
 *
 * @date Created in 2021-04-07 19:03
 * @modified By
 */
@Slf4j
public class UserInputHandler implements Runnable{

    private ChatClient client;

    public UserInputHandler(ChatClient client) {
        this.client = client;
    }

    @Override
    public void run() {
        try {
            while (true) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(System.in));
                String str = reader.readLine();

                //发送信息到服务器
                client.send(str + "\n");

                //若是为"quit"
                if(client.quit(str)){
                    break;
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
